import TableLyout from '@/components/commonPublic/tableLyout.vue'
import TableData from "@/components/commonPublic/el-table-pagination"; //主页面列表
import ImageData from "@/components/public/imageData/index.vue"; //查看资料
import publicForm from "@/components/QualityGridManage/fromCom.vue"; //弹框form
import tree from "@/components/QualityGridManage/tree"; //左侧机构树
import elBreadcrumb from "@/components/public/el-breadcrumb.vue";
import formJs from "./regexDetail.js"; //新增弹框配置项
import {
    getUserCompanyTreeList, //树状图
    getWbsWorkStepsPageLists, //列表页接口
    GetShanXiStepFiles1,
    stationWorkToken
} from "@/api/request.js";


import {
    objectType,
    statusStep,
    imageType,
    // WorkType
} from "@/utils/config"; //配置项

// import engPage from "@/components/PCManage/engPage"; //建设项目进场材料外委情况动态统计

export default {
    name: "inspectionProcessStatus",
    data() {
        return {
            name: "inspectionProcessStatus",
            monthStatus: null,
            // 机构树
            highlightBd: true, //左侧机构树停留背景色
            treelength: false, //左侧树显示隐藏
            checked: true, //查看下级按钮
            showName: "未选择公司",
            projectId: null,
            projectName: null,
            companyTree: "", //所有数据
            treeData: [], //左侧树状图数据
            defaultProps: {
                children: "children",
                label: "fShortname",
            },
            treeText: {
                companyId: null,
                userId: null,
            },
            checkData: [], //树状图默认选中id
            btnObj: [], //按钮
            queryCriteriaData: {}, //分页
            searchObj: formJs.searchObj, //搜索条件
            table_config: formJs.table_config, //列表
            // 弹框
            isShow: false, //是否显示弹框
            currentRow: null, //列表详情
            imageData: null,
            // 弹框
            status: null, //按钮状态1新增 3编辑
            dataFid: null, //列表详情id
            // 列表
            getList: {
                fCompanyid: "", //分子公司或建设项目id
                stationType: "1",
                page: 1, // 当前页码
                pageSize: 20, // 每页显示条数
                keywords: "",
                fStatus: ""
            },
            fFileName: "",
            pdfUrl: "",
            dialogText: "",
            loading: false,
            companyId: null,
            currentRowId: "",
            tableData: []

        };
    },
    components: {
        TableLyout,
        publicForm,
        TableData,
        tree,
        elBreadcrumb,
        ImageData,
        // CustomDialog
        // engPage

    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
        this.projectId = this.userInfo.projectId
        this.projectName = this.userInfo.projectId
        // this.getList.userId = this.userInfo.projectName

        if (JSON.parse(sessionStorage.getItem("Quality_treeCheck"))) {
            var data = JSON.parse(sessionStorage.getItem("Quality_treeCheck"));
            this.checkData = [data.fCompanyid];
        }
        this.table_config.thead[0]["callback"] = function (rowData) {
            let data = objectType[rowData.FinishSign];
            if (data) {
                return data.label;
            }
        };
        this.table_config.thead[4]["callback"] = function (rowData) {
            let data = statusStep[rowData.StepState];
            if (data) {
                return data.label;
            }
        };
        // this.table_config.thead[3]["callback"] = function (rowData) {
        //     let data = WorkType[rowData.WorkType];
        //     if (data) {
        //         return data.label;
        //     }
        // };

        this.table_config.thead[5]["callback"] = function (rowData) {
            let data = objectType[rowData.IsKey];
            if (data) {
                return data.label;
            }
        };
        this.table_config.thead[6]["callback"] = function (rowData) {
            let data = objectType[rowData.IsConcealed];
            if (data) {
                return data.label;
            }
        };
        this.table_config.thead[7]["callback"] = function (rowData) {
            let data = objectType[rowData.IsDisposable];
            if (data) {
                return data.label;
            }
        };
        this.table_config.thead[10]["callback"] = function (rowData) {
            let data = imageType[rowData.files];
            if (data) {
                return data.label;
            }
        };
        this.table_config.thead[10]["specialClass"] = function (rowData) {
            let data = imageType[rowData.files];
            if (data) {
                return data.className;
            }
        };

        this.getTree();

    },
    methods: {
        // 弹框组件
        refreshList() {
            //刷新列表
            this.$nextTick(() => {
                this.yj_searchForm(this.getList);
            });
        },
        closeDialog() {
            this.isShow = false;
        },
        // 弹框组件

        // 获取token
        getToken(row) {
            var data = {
                username: "sxlq",
                password: "sxlq.123",
                client_secret: "0462e562-0c92-4b69-b577-5bde7e6d1fc3",
                client_id: "2338924428965001937",
                grant_type: "password",
                scope: "private",
                connection_id: "qyegllx",
            };
            stationWorkToken(data)
                .then((res) => {
                    this.token = res.token_type + " " + res.access_token;
                    var text = {
                        projectCode: row.ProjectCode,
                        partCode: row.WorkPartCode,
                        id: row.Id
                    };
                    this.getConcealedStepCount(text, this.token);
                })
                .catch(() => {
                    console.log("获取token失败");
                });
        },
        //获取隐蔽工序文件
        getConcealedStepCount(data, token) {
            this.imageData = [];
            var imageDataList = [];
            GetShanXiStepFiles1(data, token)
                .then((res) => {
                    if (res.success) {
                        var that = this;
                        that.isShow = true;

                        // res.data.map(item => {
                        //     if (item.IsConcealed == true) {
                        //         item.Files.map(v => {
                        //             imageDataList.push(v);
                        //         })
                        //     }
                        // });

                        res.data[0].Files.map(item => {
                            imageDataList.push(item);
                        })
                        that.imageData = imageDataList;
                    } else {
                        this.$message.error("获取隐蔽工序文件获取失败");
                    }
                })
                .catch(() => {
                    console.log("获取隐蔽工序文件接口报错");
                });
        },
        //查看下级
        see() {
            if (this.checked == true) {
                this.getList.sublevel = 1;
            } else {
                this.getList.sublevel = 0;
            }
            this.getDataList(this.getList);
        },

        // 左侧树结构
        getTree() {
            this.loading = true;
            this.treeText.companyId = this.userInfo.companyId;
            this.treeText.userId = this.userInfo.userId;
            getUserCompanyTreeList(this.treeText)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        var companyTree = res.data.userCompanyTreeList;
                        this.treeData = this.commonJs.tranListToTreeData(companyTree);
                        if (this.treeData[0].children == undefined) {
                            this.treelength = false; //左侧组织机构是否显示
                        } else {
                            this.treelength = true;
                        }

                        if (this.checkData[0]) {
                            this.showName = JSON.parse(
                                sessionStorage.getItem("Quality_treeCheck")
                            ).fShortname;
                            this.getList.fCompanyid = this.checkData[0];
                        } else {
                            this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
                            this.getList.fCompanyid = this.treeData[0].fCompanyid; //当前点击组织机构ID
                            this.checkData[0] = this.treeData[0].fCompanyid;
                        }
                        this.yj_searchForm(this.getList); // 调用主页面列表接口
                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 点击左侧机构树
        handleNodeClick(data) {
            this.showName = data.fShortname; //顶部显示机构名
            this.getList.fCompanyid = data.fCompanyid;
            this.getList.page = 1;
            this.getList.pageSize = 20;
            this.yj_searchForm(this.getList);
        },

        // 点击查看
        yj_viewForm(row, label) {
            this.currentRow = row;
            if (label == "影像资料") {
                this.getToken(row);
            }
        },
        // 点击当前行变色
        rowChange(val) {
            this.currentRow = val;
            this.dataFid = val.fId;
        },
        // 模糊查询
        fuzzyQuery(data) {
            if (data.fStatus == "工序状态") {
                data.fStatus = "";
            }
            if (data.keywords == "隐蔽工序类别") {
                data.keywords = "";
            }

            data.page = 1;
            this.yj_searchForm(data);
        },
        // 获取列表数据
        yj_searchForm(data) {
            this.loading = true;
            var that = this;
            var obj = {
                orgCode: data.fCompanyid, //必传 传标段id(目前只有青银二广点击标段有数据)(1标:a1d978df-6665-4836-b892-5368af07dedd 2标:4ec357bf-6fbb-474d-9345-e57bad661ce7 3标:f8c992c1-a9e9-4d10-a784-4f3b9f0b588d)
                stationType: "1", //是否为隐蔽工序 传1查隐蔽 不查传0或不传
                page: data.page, // 当前页码
                pageSize: data.pageSize, // 每页显示条数
                keyWord: data.keywords,
                fStatus: data.fStatus,//工序状态
            }
            getWbsWorkStepsPageLists(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        this.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },
    },
};